package com.ssm.zy.handler;

import com.ssm.zy.common.exception.LoginException;
import com.ssm.zy.common.result.Result;
import com.ssm.zy.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 郑智辰~
 * @version 1.0
 *
 * 全局异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value = {LoginException.class})
    public Result handleLoginException(HttpServletRequest request, HttpServletResponse response,
                                       Exception e){

        LoginException loginException = (LoginException) e;
        logger.error("登录失败,具体原因:{}",loginException.getMessage());

        return Result.err(ResultCode.LOGIN_ERROR);
    }


    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result handleException(HttpServletRequest request, HttpServletResponse response,
                                       Exception e){
        e.printStackTrace();

        return Result.err(ResultCode.ERROR);
    }
}
